//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package ming.util.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.lang.StringUtils;
import org.apache.fop.svg.PDFTranscoder;

public class HighchartsUtil extends HttpServlet {
    public HighchartsUtil() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String type = request.getParameter("type");
        String svg = request.getParameter("svg");
        String filename = request.getParameter("filename");
        filename = filename == null ? "chart" : filename;
        System.out.println("===" + type + "|" + svg + "|" + filename);
        String ext;
        if (type == null || svg == null) {
            BufferedReader rader = request.getReader();
            int i = 0;

            while((ext = rader.readLine()) != null) {
                ++i;
                if (i == 4) {
                    filename = ext;
                } else if (i == 8) {
                    type = ext;
                } else if (i == 20) {
                    svg = ext;
                    break;
                }
            }
        }

        ServletOutputStream out = response.getOutputStream();
        if (!StringUtils.isBlank(type) && !StringUtils.isBlank(svg)) {
            svg = svg.replaceAll(":rect", "rect");
            ext = "";
            Transcoder t = null;
            if (type.equals("image/png")) {
                ext = "png";
                t = new PNGTranscoder();
            } else if (type.equals("image/jpeg")) {
                ext = "jpg";
                t = new JPEGTranscoder();
                ((Transcoder)t).addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(0.8D));
            } else if (type.equals("application/pdf")) {
                ext = "pdf";
                t = new PDFTranscoder();
            } else if (type.equals("image/svg+xml")) {
                ext = "svg";
            }

            response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename + "." + ext, "UTF-8"));
            response.addHeader("Content-Type", type);
            if (t != null) {
                TranscoderInput input = new TranscoderInput(new StringReader(svg));
                TranscoderOutput output = new TranscoderOutput(out);

                try {
                    ((Transcoder)t).transcode(input, output);
                } catch (TranscoderException var12) {
                    out.print("Problem transcoding stream. See the web logs for more details.");
                    var12.printStackTrace();
                }
            } else if (ext.equals("svg")) {
                OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
                writer.append(svg);
                writer.close();
            } else {
                out.print("Invalid type: " + type);
            }
        } else {
            response.addHeader("Content-Type", "text/html");
            out.println("Usage:\n\tParameter [svg]: The DOM Element to be converted.\n\tParameter [type]: The destination MIME type for the elment to be transcoded.");
        }

        out.flush();
        out.close();
    }
}
